﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.ComponentModel;
using personalplaner.business.extensions;

namespace personalplaner.business.common
{
	[Serializable]
	public class LoginData : INotifyPropertyChanged
	{
		#region INotifyPropertyChanged Members

        [field: NonSerialized]
		public event PropertyChangedEventHandler PropertyChanged;// = delegate { };

		protected void RaisePropertyChanged(string propertyName)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}

		#endregion

		bool showLogin;
		string dsn;
		string database;

		public LoginData()
		{
		}

		public string Database
		{
			get { return database; }
			set
			{
				database = value;
				RaisePropertyChanged("Database");
			}
		}

		public string DSN
		{
			get { return dsn; }
			set
			{
				dsn = value;
				RaisePropertyChanged("DSN");
			}
		}

		string password = "personalplaner";
		public string Password
		{
			get { return password; }
			set
			{
				password = value;
				RaisePropertyChanged("Password");
			}
		}

		string userName = "prsadmin";
		public string UserName
		{
			get { return userName; }
			set
			{
				userName = value;
				RaisePropertyChanged("UserName");
			}
		}

        bool _savePassword = true;
        public bool SavePassword
        {
            get
            {
                return _savePassword;
            }
            set
            {
                _savePassword = value;
                RaisePropertyChanged("SavePassword");
            }
        }

		public bool ShowLogin
		{
			get { return showLogin; }
			set
			{
				showLogin = value;
				RaisePropertyChanged("ShowLogin");
			}
		}


		/// <summary>
		/// Serializes LoginData object
		/// Stores all the Sql-Data
		/// </summary>
		/// <param name="data">LoginData object</param>
		public static void Serialize(LoginData data)
		{
			Stream stream = null;
			try
			{
                var clone = data.Clone<LoginData>();
                if (!clone.SavePassword)
                    clone.Password = "";

				IFormatter formatter = new BinaryFormatter();
				stream = new FileStream("data.ser", FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, clone);
			}
			catch
			{
				// do nothing, just ignore any possible errors
			}
			finally
			{
				if (null != stream)
					stream.Close();
			}
		}

		/// <summary>
		/// Deserializes the data.ser object
		/// Contains all sql login data
		/// </summary>
		/// <returns>LoginData object containing all sql login informations</returns>
		public static LoginData Deserialize()
		{
			Stream stream = null;
			LoginData data = null;
			try
			{
				IFormatter formatter = new BinaryFormatter();
				stream = new FileStream("data.ser", FileMode.Open, FileAccess.Read, FileShare.None);
				data = (LoginData)formatter.Deserialize(stream);
			}
			catch
			{
			} // do nothing, just ignore any possible errors

			finally
			{
				if (null != stream)
					stream.Close();
			}

			return data;
		}
	}
}
